{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Restructuring Data into a Tidy Form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "pd.set_option('max_columns', 4, 'max_rows', 10, 'max_colwidth', 12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying variable values as column names with stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Texas</th>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Arizona</th>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Florida</th>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Apple  Orange  Banana\n",
       "Texas       12      10      40\n",
       "Arizona      9       7      12\n",
       "Florida      0      14     190"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit = pd.read_csv('data/state_fruit.csv', index_col=0)\n",
    "state_fruit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>level_0</th>\n",
       "      <th>level_1</th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   level_0 level_1    0\n",
       "0    Texas   Apple   12\n",
       "1    Texas  Orange   10\n",
       "2    Texas  Banana   40\n",
       "3  Arizona   Apple    9\n",
       "4  Arizona  Orange    7\n",
       "5  Arizona  Banana   12\n",
       "6  Florida   Apple    0\n",
       "7  Florida  Orange   14\n",
       "8  Florida  Banana  190"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(state_fruit\n",
    "   .stack()\n",
    "   .reset_index()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>fruit</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     state   fruit  weight\n",
       "0    Texas   Apple      12\n",
       "1    Texas  Orange      10\n",
       "2    Texas  Banana      40\n",
       "3  Arizona   Apple       9\n",
       "4  Arizona  Orange       7\n",
       "5  Arizona  Banana      12\n",
       "6  Florida   Apple       0\n",
       "7  Florida  Orange      14\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(state_fruit\n",
    "   .stack()\n",
    "   .reset_index()\n",
    "   .rename(columns={'level_0':'state', \n",
    "      'level_1': 'fruit', 0: 'weight'})\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "state    fruit \n",
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(state_fruit\n",
    "    .stack()\n",
    "    .rename_axis(['state', 'fruit'])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>fruit</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     state   fruit  weight\n",
       "0    Texas   Apple      12\n",
       "1    Texas  Orange      10\n",
       "2    Texas  Banana      40\n",
       "3  Arizona   Apple       9\n",
       "4  Arizona  Orange       7\n",
       "5  Arizona  Banana      12\n",
       "6  Florida   Apple       0\n",
       "7  Florida  Orange      14\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(state_fruit\n",
    "    .stack()\n",
    "    .rename_axis(['state', 'fruit'])\n",
    "    .reset_index(name='weight')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State  Apple  Orange  Banana\n",
       "0    Texas     12      10      40\n",
       "1  Arizona      9       7      12\n",
       "2  Florida      0      14     190"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2 = pd.read_csv('data/state_fruit2.csv')\n",
    "state_fruit2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  State       Texas\n",
       "   Apple          12\n",
       "   Orange         10\n",
       "   Banana         40\n",
       "1  State     Arizona\n",
       "              ...   \n",
       "   Banana         12\n",
       "2  State     Florida\n",
       "   Apple           0\n",
       "   Orange         14\n",
       "   Banana        190\n",
       "Length: 12, dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "State          \n",
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.set_index('State').stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying variable values as column names with melt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State  Apple  Orange  Banana\n",
       "0    Texas     12      10      40\n",
       "1  Arizona      9       7      12\n",
       "2  Florida      0      14     190"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2 = pd.read_csv('data/state_fruit2.csv')\n",
    "state_fruit2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State variable  value\n",
       "0    Texas    Apple     12\n",
       "1  Arizona    Apple      9\n",
       "2  Florida    Apple      0\n",
       "3    Texas   Orange     10\n",
       "4  Arizona   Orange      7\n",
       "5  Florida   Orange     14\n",
       "6    Texas   Banana     40\n",
       "7  Arizona   Banana     12\n",
       "8  Florida   Banana    190"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars=['State'],\n",
    "    value_vars=['Apple', 'Orange', 'Banana'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Fruit</th>\n",
       "      <th>Weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State   Fruit  Weight\n",
       "0    Texas   Apple      12\n",
       "1  Arizona   Apple       9\n",
       "2  Florida   Apple       0\n",
       "3    Texas  Orange      10\n",
       "4  Arizona  Orange       7\n",
       "5  Florida  Orange      14\n",
       "6    Texas  Banana      40\n",
       "7  Arizona  Banana      12\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars=['State'],\n",
    "                   value_vars=['Apple', 'Orange', 'Banana'],\n",
    "                   var_name='Fruit',\n",
    "                   value_name='Weight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>State</td>\n",
       "      <td>Texas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>State</td>\n",
       "      <td>Arizona</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>State</td>\n",
       "      <td>Florida</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>12 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   variable    value\n",
       "0     State    Texas\n",
       "1     State  Arizona\n",
       "2     State  Florida\n",
       "3     Apple       12\n",
       "4     Apple        9\n",
       "..      ...      ...\n",
       "7    Orange        7\n",
       "8    Orange       14\n",
       "9    Banana       40\n",
       "10   Banana       12\n",
       "11   Banana      190\n",
       "\n",
       "[12 rows x 2 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State variable  value\n",
       "0    Texas    Apple     12\n",
       "1  Arizona    Apple      9\n",
       "2  Florida    Apple      0\n",
       "3    Texas   Orange     10\n",
       "4  Arizona   Orange      7\n",
       "5  Florida   Orange     14\n",
       "6    Texas   Banana     40\n",
       "7  Arizona   Banana     12\n",
       "8  Florida   Banana    190"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars='State')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stacking multiple groups of variables simultaneously"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_1_name</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>actor_3_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates ...</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark...</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>...</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star War...</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>...</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   movie_title actor_1_name  ... actor_2_facebook_likes actor_3_facebook_likes\n",
       "0       Avatar  CCH Pounder  ...        936.0                  855.0          \n",
       "1  Pirates ...  Johnny Depp  ...       5000.0                 1000.0          \n",
       "2      Spectre  Christop...  ...        393.0                  161.0          \n",
       "3  The Dark...    Tom Hardy  ...      23000.0                23000.0          \n",
       "4  Star War...  Doug Walker  ...         12.0                    NaN          \n",
       "\n",
       "[5 rows x 7 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "actor = movie[['movie_title', 'actor_1_name',\n",
    "               'actor_2_name', 'actor_3_name',\n",
    "               'actor_1_facebook_likes',\n",
    "               'actor_2_facebook_likes',\n",
    "               'actor_3_facebook_likes']]\n",
    "actor.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def change_col_name(col_name):\n",
    "    col_name = col_name.replace('_name', '')\n",
    "    if 'facebook' in col_name:\n",
    "        fb_idx = col_name.find('facebook')\n",
    "        col_name = (col_name[:5] + col_name[fb_idx - 1:] \n",
    "               + col_name[5:fb_idx-1])\n",
    "    return col_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_1</th>\n",
       "      <th>...</th>\n",
       "      <th>actor_facebook_likes_2</th>\n",
       "      <th>actor_facebook_likes_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>...</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates ...</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>...</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark...</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>...</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star War...</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>...</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4911</th>\n",
       "      <td>Signed S...</td>\n",
       "      <td>Eric Mabius</td>\n",
       "      <td>...</td>\n",
       "      <td>470.0</td>\n",
       "      <td>318.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4912</th>\n",
       "      <td>The Foll...</td>\n",
       "      <td>Natalie Zea</td>\n",
       "      <td>...</td>\n",
       "      <td>593.0</td>\n",
       "      <td>319.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4913</th>\n",
       "      <td>A Plague...</td>\n",
       "      <td>Eva Boehnke</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4914</th>\n",
       "      <td>Shanghai...</td>\n",
       "      <td>Alan Ruck</td>\n",
       "      <td>...</td>\n",
       "      <td>719.0</td>\n",
       "      <td>489.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4915</th>\n",
       "      <td>My Date ...</td>\n",
       "      <td>John August</td>\n",
       "      <td>...</td>\n",
       "      <td>23.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4916 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      movie_title      actor_1  ... actor_facebook_likes_2  \\\n",
       "0          Avatar  CCH Pounder  ...        936.0             \n",
       "1     Pirates ...  Johnny Depp  ...       5000.0             \n",
       "2         Spectre  Christop...  ...        393.0             \n",
       "3     The Dark...    Tom Hardy  ...      23000.0             \n",
       "4     Star War...  Doug Walker  ...         12.0             \n",
       "...           ...          ...  ...          ...             \n",
       "4911  Signed S...  Eric Mabius  ...        470.0             \n",
       "4912  The Foll...  Natalie Zea  ...        593.0             \n",
       "4913  A Plague...  Eva Boehnke  ...          0.0             \n",
       "4914  Shanghai...    Alan Ruck  ...        719.0             \n",
       "4915  My Date ...  John August  ...         23.0             \n",
       "\n",
       "     actor_facebook_likes_3  \n",
       "0           855.0            \n",
       "1          1000.0            \n",
       "2           161.0            \n",
       "3         23000.0            \n",
       "4             NaN            \n",
       "...           ...            \n",
       "4911        318.0            \n",
       "4912        319.0            \n",
       "4913          0.0            \n",
       "4914        489.0            \n",
       "4915         16.0            \n",
       "\n",
       "[4916 rows x 7 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor2 = actor.rename(columns=change_col_name)\n",
    "actor2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>actor</th>\n",
       "      <th>actor_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_num</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <th>1</th>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <th>1</th>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <th>1</th>\n",
       "      <td>Christop...</td>\n",
       "      <td>11000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <th>1</th>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>27000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <th>1</th>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              actor  actor_facebook_likes\n",
       "movie_title  actor_num                                   \n",
       "Avatar       1          CCH Pounder       1000.0         \n",
       "Pirates o... 1          Johnny Depp      40000.0         \n",
       "Spectre      1          Christop...      11000.0         \n",
       "The Dark ... 1            Tom Hardy      27000.0         \n",
       "Star Wars... 1          Doug Walker        131.0         "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stubs = ['actor', 'actor_facebook_likes']\n",
    "actor2_tidy = pd.wide_to_long(actor2,\n",
    "    stubnames=stubs,\n",
    "    i=['movie_title'],\n",
    "    j='actor_num',\n",
    "    sep='_')\n",
    "actor2_tidy.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>...</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TX</td>\n",
       "      <td>US</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MA</td>\n",
       "      <td>US</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ON</td>\n",
       "      <td>CAN</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  State Country  ...  d  e\n",
       "0    TX      US  ...  2  6\n",
       "1    MA      US  ...  9  7\n",
       "2    ON     CAN  ...  4  2\n",
       "\n",
       "[3 rows x 7 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/stackme.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>...</th>\n",
       "      <th>group2_a1</th>\n",
       "      <th>group2_b2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TX</td>\n",
       "      <td>US</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MA</td>\n",
       "      <td>US</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ON</td>\n",
       "      <td>CAN</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  State Country  ...  group2_a1  group2_b2\n",
       "0    TX      US  ...          2          6\n",
       "1    MA      US  ...          9          7\n",
       "2    ON     CAN  ...          4          2\n",
       "\n",
       "[3 rows x 7 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.rename(columns = {'a1':'group1_a1', 'b2':'group1_b2',\n",
    "                     'd':'group2_a1', 'e':'group2_b2'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>group1</th>\n",
       "      <th>group2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>Test</th>\n",
       "      <th>Label</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">TX</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">US</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test1</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.45</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>0.30</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">MA</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">US</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test2</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.03</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>1.20</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">ON</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">CAN</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test3</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.70</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>4.20</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           group1  group2\n",
       "State Country Test  Label                \n",
       "TX    US      Test1 a1       0.45       2\n",
       "                    b2       0.30       6\n",
       "MA    US      Test2 a1       0.03       9\n",
       "                    b2       1.20       7\n",
       "ON    CAN     Test3 a1       0.70       4\n",
       "                    b2       4.20       2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.wide_to_long(\n",
    "       df.rename(columns = {'a1':'group1_a1', \n",
    "                 'b2':'group1_b2',\n",
    "                 'd':'group2_a1', 'e':'group2_b2'}),\n",
    "    stubnames=['group1', 'group2'],\n",
    "    i=['State', 'Country', 'Test'],\n",
    "    j='Label',\n",
    "    suffix='.+',\n",
    "    sep='_')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inverting stacked data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>...</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Amridge University</th>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alabama State University</th>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SAE Institute of Technology  San Francisco</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rasmussen College - Overland Park</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>National Personal Training Institute of Cleveland</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bay Area Medical Academy - San Jose Satellite Location</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Excel Learning Center-San Antonio South</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7535 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              UGDS_WHITE  UGDS_BLACK  ...  UGDS_NRA  UGDS_UNKN\n",
       "INSTNM                                ...                     \n",
       "Alabama A...      0.0333      0.9353  ...    0.0059     0.0138\n",
       "Universit...      0.5922      0.2600  ...    0.0179     0.0100\n",
       "Amridge U...      0.2990      0.4192  ...    0.0000     0.2715\n",
       "Universit...      0.6988      0.1255  ...    0.0332     0.0350\n",
       "Alabama S...      0.0158      0.9208  ...    0.0243     0.0137\n",
       "...                  ...         ...  ...       ...        ...\n",
       "SAE Insti...         NaN         NaN  ...       NaN        NaN\n",
       "Rasmussen...         NaN         NaN  ...       NaN        NaN\n",
       "National ...         NaN         NaN  ...       NaN        NaN\n",
       "Bay Area ...         NaN         NaN  ...       NaN        NaN\n",
       "Excel Lea...         NaN         NaN  ...       NaN        NaN\n",
       "\n",
       "[7535 rows x 9 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "usecol_func = lambda x: 'UGDS_' in x or x == 'INSTNM'\n",
    "college = pd.read_csv('data/college.csv',\n",
    "    index_col='INSTNM',\n",
    "    usecols=usecol_func)\n",
    "college"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "INSTNM                                     \n",
       "Alabama A & M University         UGDS_WHITE    0.0333\n",
       "                                 UGDS_BLACK    0.9353\n",
       "                                 UGDS_HISP     0.0055\n",
       "                                 UGDS_ASIAN    0.0019\n",
       "                                 UGDS_AIAN     0.0024\n",
       "                                                ...  \n",
       "Coastal Pines Technical College  UGDS_AIAN     0.0034\n",
       "                                 UGDS_NHPI     0.0017\n",
       "                                 UGDS_2MOR     0.0191\n",
       "                                 UGDS_NRA      0.0028\n",
       "                                 UGDS_UNKN     0.0056\n",
       "Length: 61866, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_stacked = college.stack()\n",
    "college_stacked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>...</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Amridge University</th>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alabama State University</th>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hollywood Institute of Beauty Careers-West Palm Beach</th>\n",
       "      <td>0.2182</td>\n",
       "      <td>0.4182</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0182</td>\n",
       "      <td>0.0909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hollywood Institute of Beauty Careers-Casselberry</th>\n",
       "      <td>0.1200</td>\n",
       "      <td>0.3333</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Coachella Valley Beauty College-Beaumont</th>\n",
       "      <td>0.3284</td>\n",
       "      <td>0.1045</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dewey University-Mayaguez</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Coastal Pines Technical College</th>\n",
       "      <td>0.6762</td>\n",
       "      <td>0.2508</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0028</td>\n",
       "      <td>0.0056</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6874 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              UGDS_WHITE  UGDS_BLACK  ...  UGDS_NRA  UGDS_UNKN\n",
       "INSTNM                                ...                     \n",
       "Alabama A...      0.0333      0.9353  ...    0.0059     0.0138\n",
       "Universit...      0.5922      0.2600  ...    0.0179     0.0100\n",
       "Amridge U...      0.2990      0.4192  ...    0.0000     0.2715\n",
       "Universit...      0.6988      0.1255  ...    0.0332     0.0350\n",
       "Alabama S...      0.0158      0.9208  ...    0.0243     0.0137\n",
       "...                  ...         ...  ...       ...        ...\n",
       "Hollywood...      0.2182      0.4182  ...    0.0182     0.0909\n",
       "Hollywood...      0.1200      0.3333  ...    0.0000     0.0667\n",
       "Coachella...      0.3284      0.1045  ...    0.0000     0.0000\n",
       "Dewey Uni...      0.0000      0.0000  ...    0.0000     0.0000\n",
       "Coastal P...      0.6762      0.2508  ...    0.0028     0.0056\n",
       "\n",
       "[6874 rows x 9 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_stacked.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>...</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama ...</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Universi...</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge ...</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Universi...</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama ...</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7530</th>\n",
       "      <td>SAE Inst...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7531</th>\n",
       "      <td>Rasmusse...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7532</th>\n",
       "      <td>National...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7533</th>\n",
       "      <td>Bay Area...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7534</th>\n",
       "      <td>Excel Le...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7535 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           INSTNM  UGDS_WHITE  ...  UGDS_NRA  UGDS_UNKN\n",
       "0     Alabama ...      0.0333  ...    0.0059     0.0138\n",
       "1     Universi...      0.5922  ...    0.0179     0.0100\n",
       "2     Amridge ...      0.2990  ...    0.0000     0.2715\n",
       "3     Universi...      0.6988  ...    0.0332     0.0350\n",
       "4     Alabama ...      0.0158  ...    0.0243     0.0137\n",
       "...           ...         ...  ...       ...        ...\n",
       "7530  SAE Inst...         NaN  ...       NaN        NaN\n",
       "7531  Rasmusse...         NaN  ...       NaN        NaN\n",
       "7532  National...         NaN  ...       NaN        NaN\n",
       "7533  Bay Area...         NaN  ...       NaN        NaN\n",
       "7534  Excel Le...         NaN  ...       NaN        NaN\n",
       "\n",
       "[7535 rows x 10 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2 = pd.read_csv('data/college.csv',\n",
    "   usecols=usecol_func)\n",
    "college2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>Race</th>\n",
       "      <th>Percentage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama ...</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.0333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Universi...</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.5922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge ...</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.2990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Universi...</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.6988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama ...</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.0158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67810</th>\n",
       "      <td>SAE Inst...</td>\n",
       "      <td>UGDS_UNKN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67811</th>\n",
       "      <td>Rasmusse...</td>\n",
       "      <td>UGDS_UNKN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67812</th>\n",
       "      <td>National...</td>\n",
       "      <td>UGDS_UNKN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67813</th>\n",
       "      <td>Bay Area...</td>\n",
       "      <td>UGDS_UNKN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67814</th>\n",
       "      <td>Excel Le...</td>\n",
       "      <td>UGDS_UNKN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67815 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            INSTNM        Race  Percentage\n",
       "0      Alabama ...  UGDS_WHITE      0.0333\n",
       "1      Universi...  UGDS_WHITE      0.5922\n",
       "2      Amridge ...  UGDS_WHITE      0.2990\n",
       "3      Universi...  UGDS_WHITE      0.6988\n",
       "4      Alabama ...  UGDS_WHITE      0.0158\n",
       "...            ...         ...         ...\n",
       "67810  SAE Inst...   UGDS_UNKN         NaN\n",
       "67811  Rasmusse...   UGDS_UNKN         NaN\n",
       "67812  National...   UGDS_UNKN         NaN\n",
       "67813  Bay Area...   UGDS_UNKN         NaN\n",
       "67814  Excel Le...   UGDS_UNKN         NaN\n",
       "\n",
       "[67815 rows x 3 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_melted = college2.melt(id_vars='INSTNM',\n",
    "    var_name='Race',\n",
    "    value_name='Percentage')\n",
    "college_melted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Race</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>...</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A &amp; W Healthcare Educators</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A T Still University of Health Sciences</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC Beauty Academy</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC Beauty College Inc</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AI Miami International University of Art and Design</th>\n",
       "      <td>0.0018</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.4644</td>\n",
       "      <td>0.0324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Yukon Beauty College Inc</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.1200</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.8000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Z Hair Academy</th>\n",
       "      <td>0.0211</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0105</td>\n",
       "      <td>0.9368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Zane State College</th>\n",
       "      <td>0.0218</td>\n",
       "      <td>0.0029</td>\n",
       "      <td>...</td>\n",
       "      <td>0.2399</td>\n",
       "      <td>0.6995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>duCret School of Arts</th>\n",
       "      <td>0.0976</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0244</td>\n",
       "      <td>0.4634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eClips School of Cosmetology and Barbering</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.1446</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7535 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Race          UGDS_2MOR  UGDS_AIAN  ...  UGDS_UNKN  UGDS_WHITE\n",
       "INSTNM                              ...                       \n",
       "A & W Hea...     0.0000     0.0000  ...     0.0000      0.0000\n",
       "A T Still...        NaN        NaN  ...        NaN         NaN\n",
       "ABC Beaut...     0.0000     0.0000  ...     0.0000      0.0000\n",
       "ABC Beaut...     0.0000     0.0000  ...     0.0000      0.2895\n",
       "AI Miami ...     0.0018     0.0000  ...     0.4644      0.0324\n",
       "...                 ...        ...  ...        ...         ...\n",
       "Yukon Bea...     0.0000     0.1200  ...     0.0000      0.8000\n",
       "Z Hair Ac...     0.0211     0.0000  ...     0.0105      0.9368\n",
       "Zane Stat...     0.0218     0.0029  ...     0.2399      0.6995\n",
       "duCret Sc...     0.0976     0.0000  ...     0.0244      0.4634\n",
       "eClips Sc...     0.0000     0.0000  ...     0.0000      0.1446\n",
       "\n",
       "[7535 rows x 9 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted_inv = college_melted.pivot(index='INSTNM',\n",
    "    columns='Race',\n",
    "    values='Percentage')\n",
    "melted_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2_replication = (melted_inv\n",
    "    .loc[college2['INSTNM'], college2.columns[1:]]\n",
    "    .reset_index()\n",
    ")\n",
    "college2.equals(college2_replication)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>INSTNM</th>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <th>...</th>\n",
       "      <th>Dewey University-Mayaguez</th>\n",
       "      <th>Coastal Pines Technical College</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.6762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0056</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9 rows × 6874 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "INSTNM      Alabama A & M University  University of Alabama at Birmingham  \\\n",
       "UGDS_WHITE       0.0333                    0.5922                           \n",
       "UGDS_BLACK       0.9353                    0.2600                           \n",
       "UGDS_HISP        0.0055                    0.0283                           \n",
       "UGDS_ASIAN       0.0019                    0.0518                           \n",
       "UGDS_AIAN        0.0024                    0.0022                           \n",
       "UGDS_NHPI        0.0019                    0.0007                           \n",
       "UGDS_2MOR        0.0000                    0.0368                           \n",
       "UGDS_NRA         0.0059                    0.0179                           \n",
       "UGDS_UNKN        0.0138                    0.0100                           \n",
       "\n",
       "INSTNM      ...  Dewey University-Mayaguez  Coastal Pines Technical College  \n",
       "UGDS_WHITE  ...          0.0                     0.6762                      \n",
       "UGDS_BLACK  ...          0.0                     0.2508                      \n",
       "UGDS_HISP   ...          1.0                     0.0359                      \n",
       "UGDS_ASIAN  ...          0.0                     0.0045                      \n",
       "UGDS_AIAN   ...          0.0                     0.0034                      \n",
       "UGDS_NHPI   ...          0.0                     0.0017                      \n",
       "UGDS_2MOR   ...          0.0                     0.0191                      \n",
       "UGDS_NRA    ...          0.0                     0.0028                      \n",
       "UGDS_UNKN   ...          0.0                     0.0056                      \n",
       "\n",
       "[9 rows x 6874 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.stack().unstack(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>INSTNM</th>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <th>...</th>\n",
       "      <th>Bay Area Medical Academy - San Jose Satellite Location</th>\n",
       "      <th>Excel Learning Center-San Antonio South</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9 rows × 7535 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "INSTNM      Alabama A & M University  University of Alabama at Birmingham  \\\n",
       "UGDS_WHITE       0.0333                    0.5922                           \n",
       "UGDS_BLACK       0.9353                    0.2600                           \n",
       "UGDS_HISP        0.0055                    0.0283                           \n",
       "UGDS_ASIAN       0.0019                    0.0518                           \n",
       "UGDS_AIAN        0.0024                    0.0022                           \n",
       "UGDS_NHPI        0.0019                    0.0007                           \n",
       "UGDS_2MOR        0.0000                    0.0368                           \n",
       "UGDS_NRA         0.0059                    0.0179                           \n",
       "UGDS_UNKN        0.0138                    0.0100                           \n",
       "\n",
       "INSTNM      ...  Bay Area Medical Academy - San Jose Satellite Location  \\\n",
       "UGDS_WHITE  ...          NaN                                              \n",
       "UGDS_BLACK  ...          NaN                                              \n",
       "UGDS_HISP   ...          NaN                                              \n",
       "UGDS_ASIAN  ...          NaN                                              \n",
       "UGDS_AIAN   ...          NaN                                              \n",
       "UGDS_NHPI   ...          NaN                                              \n",
       "UGDS_2MOR   ...          NaN                                              \n",
       "UGDS_NRA    ...          NaN                                              \n",
       "UGDS_UNKN   ...          NaN                                              \n",
       "\n",
       "INSTNM      Excel Learning Center-San Antonio South  \n",
       "UGDS_WHITE          NaN                              \n",
       "UGDS_BLACK          NaN                              \n",
       "UGDS_HISP           NaN                              \n",
       "UGDS_ASIAN          NaN                              \n",
       "UGDS_AIAN           NaN                              \n",
       "UGDS_NHPI           NaN                              \n",
       "UGDS_2MOR           NaN                              \n",
       "UGDS_NRA            NaN                              \n",
       "UGDS_UNKN           NaN                              \n",
       "\n",
       "[9 rows x 7535 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.T\n",
    "college.transpose()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unstacking after a groupby aggregation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RACE\n",
       "American Indian or Alaskan Native    60272\n",
       "Asian/Pacific Islander               61660\n",
       "Black or African American            50137\n",
       "Hispanic/Latino                      52345\n",
       "Others                               51278\n",
       "White                                64419\n",
       "Name: BASE_SALARY, dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee = pd.read_csv('data/employee.csv')\n",
    "(employee\n",
    "    .groupby('RACE')\n",
    "    ['BASE_SALARY']\n",
    "    .mean()\n",
    "    .astype(int)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RACE                               GENDER\n",
       "American Indian or Alaskan Native  Female    60238\n",
       "                                   Male      60305\n",
       "Asian/Pacific Islander             Female    63226\n",
       "                                   Male      61033\n",
       "Black or African American          Female    48915\n",
       "                                             ...  \n",
       "Hispanic/Latino                    Male      54782\n",
       "Others                             Female    63785\n",
       "                                   Male      38771\n",
       "White                              Female    66793\n",
       "                                   Male      63940\n",
       "Name: BASE_SALARY, Length: 12, dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(employee\n",
    "    .groupby(['RACE', 'GENDER'])\n",
    "    ['BASE_SALARY'] \n",
    "    .mean()\n",
    "    .astype(int)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>GENDER</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RACE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>American Indian or Alaskan Native</th>\n",
       "      <td>60238</td>\n",
       "      <td>60305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asian/Pacific Islander</th>\n",
       "      <td>63226</td>\n",
       "      <td>61033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Black or African American</th>\n",
       "      <td>48915</td>\n",
       "      <td>51082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hispanic/Latino</th>\n",
       "      <td>46503</td>\n",
       "      <td>54782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Others</th>\n",
       "      <td>63785</td>\n",
       "      <td>38771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>White</th>\n",
       "      <td>66793</td>\n",
       "      <td>63940</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "GENDER        Female   Male\n",
       "RACE                       \n",
       "American ...   60238  60305\n",
       "Asian/Pac...   63226  61033\n",
       "Black or ...   48915  51082\n",
       "Hispanic/...   46503  54782\n",
       "Others         63785  38771\n",
       "White          66793  63940"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(employee\n",
    "    .groupby(['RACE', 'GENDER'])\n",
    "    ['BASE_SALARY'] \n",
    "    .mean()\n",
    "    .astype(int)\n",
    "    .unstack('GENDER')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>RACE</th>\n",
       "      <th>American Indian or Alaskan Native</th>\n",
       "      <th>Asian/Pacific Islander</th>\n",
       "      <th>...</th>\n",
       "      <th>Others</th>\n",
       "      <th>White</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENDER</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Female</th>\n",
       "      <td>60238</td>\n",
       "      <td>63226</td>\n",
       "      <td>...</td>\n",
       "      <td>63785</td>\n",
       "      <td>66793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>60305</td>\n",
       "      <td>61033</td>\n",
       "      <td>...</td>\n",
       "      <td>38771</td>\n",
       "      <td>63940</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "RACE    American Indian or Alaskan Native  Asian/Pacific Islander  ...  \\\n",
       "GENDER                                                             ...   \n",
       "Female        60238                              63226             ...   \n",
       "Male          60305                              61033             ...   \n",
       "\n",
       "RACE    Others  White  \n",
       "GENDER                 \n",
       "Female   63785  66793  \n",
       "Male     38771  63940  \n",
       "\n",
       "[2 rows x 6 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(employee\n",
    "    .groupby(['RACE', 'GENDER'])\n",
    "    ['BASE_SALARY'] \n",
    "    .mean()\n",
    "    .astype(int)\n",
    "    .unstack('RACE')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>min</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RACE</th>\n",
       "      <th>GENDER</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">American Indian or Alaskan Native</th>\n",
       "      <th>Female</th>\n",
       "      <td>60238</td>\n",
       "      <td>98536</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>60305</td>\n",
       "      <td>81239</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Asian/Pacific Islander</th>\n",
       "      <th>Female</th>\n",
       "      <td>63226</td>\n",
       "      <td>130416</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>61033</td>\n",
       "      <td>163228</td>\n",
       "      <td>27914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Black or African American</th>\n",
       "      <th>Female</th>\n",
       "      <td>48915</td>\n",
       "      <td>150416</td>\n",
       "      <td>24960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hispanic/Latino</th>\n",
       "      <th>Male</th>\n",
       "      <td>54782</td>\n",
       "      <td>165216</td>\n",
       "      <td>26104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Others</th>\n",
       "      <th>Female</th>\n",
       "      <td>63785</td>\n",
       "      <td>63785</td>\n",
       "      <td>63785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>38771</td>\n",
       "      <td>38771</td>\n",
       "      <td>38771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">White</th>\n",
       "      <th>Female</th>\n",
       "      <td>66793</td>\n",
       "      <td>178331</td>\n",
       "      <td>27955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>63940</td>\n",
       "      <td>210588</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>12 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      mean     max    min\n",
       "RACE         GENDER                      \n",
       "American ... Female  60238   98536  26125\n",
       "             Male    60305   81239  26125\n",
       "Asian/Pac... Female  63226  130416  26125\n",
       "             Male    61033  163228  27914\n",
       "Black or ... Female  48915  150416  24960\n",
       "...                    ...     ...    ...\n",
       "Hispanic/... Male    54782  165216  26104\n",
       "Others       Female  63785   63785  63785\n",
       "             Male    38771   38771  38771\n",
       "White        Female  66793  178331  27955\n",
       "             Male    63940  210588  26125\n",
       "\n",
       "[12 rows x 3 columns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(employee\n",
    "    .groupby(['RACE', 'GENDER'])\n",
    "    ['BASE_SALARY']\n",
    "    .agg(['mean', 'max', 'min'])\n",
    "    .astype(int)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">mean</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">min</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENDER</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "      <th>...</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RACE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>American Indian or Alaskan Native</th>\n",
       "      <td>60238</td>\n",
       "      <td>60305</td>\n",
       "      <td>...</td>\n",
       "      <td>26125</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asian/Pacific Islander</th>\n",
       "      <td>63226</td>\n",
       "      <td>61033</td>\n",
       "      <td>...</td>\n",
       "      <td>26125</td>\n",
       "      <td>27914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Black or African American</th>\n",
       "      <td>48915</td>\n",
       "      <td>51082</td>\n",
       "      <td>...</td>\n",
       "      <td>24960</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hispanic/Latino</th>\n",
       "      <td>46503</td>\n",
       "      <td>54782</td>\n",
       "      <td>...</td>\n",
       "      <td>26125</td>\n",
       "      <td>26104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Others</th>\n",
       "      <td>63785</td>\n",
       "      <td>38771</td>\n",
       "      <td>...</td>\n",
       "      <td>63785</td>\n",
       "      <td>38771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>White</th>\n",
       "      <td>66793</td>\n",
       "      <td>63940</td>\n",
       "      <td>...</td>\n",
       "      <td>27955</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               mean         ...    min       \n",
       "GENDER       Female   Male  ... Female   Male\n",
       "RACE                        ...              \n",
       "American ...  60238  60305  ...  26125  26125\n",
       "Asian/Pac...  63226  61033  ...  26125  27914\n",
       "Black or ...  48915  51082  ...  24960  26125\n",
       "Hispanic/...  46503  54782  ...  26125  26104\n",
       "Others        63785  38771  ...  63785  38771\n",
       "White         66793  63940  ...  27955  26125\n",
       "\n",
       "[6 rows x 6 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(employee\n",
    "    .groupby(['RACE', 'GENDER'])\n",
    "    ['BASE_SALARY']\n",
    "    .agg(['mean', 'max', 'min'])\n",
    "    .astype(int)\n",
    "    .unstack('GENDER')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Replicating pivot_table with a groupby aggregation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ORG_AIR</th>\n",
       "      <th>ATL</th>\n",
       "      <th>DEN</th>\n",
       "      <th>...</th>\n",
       "      <th>PHX</th>\n",
       "      <th>SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AA</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AS</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DL</th>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EV</th>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OO</th>\n",
       "      <td>3</td>\n",
       "      <td>25</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UA</th>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VX</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WN</th>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ORG_AIR  ATL  DEN  ...  PHX  SFO\n",
       "AIRLINE            ...          \n",
       "AA         3    4  ...    4    2\n",
       "AS         0    0  ...    0    0\n",
       "B6         0    0  ...    0    1\n",
       "DL        28    1  ...    1    2\n",
       "EV        18    6  ...    0    0\n",
       "...      ...  ...  ...  ...  ...\n",
       "OO         3   25  ...    9   33\n",
       "UA         2    9  ...    3   19\n",
       "US         0    0  ...    7    3\n",
       "VX         0    0  ...    0    3\n",
       "WN         9   13  ...    6   25\n",
       "\n",
       "[14 rows x 10 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights = pd.read_csv('data/flights.csv')\n",
    "fpt = flights.pivot_table(index='AIRLINE',\n",
    "    columns='ORG_AIR',\n",
    "    values='CANCELLED',\n",
    "    aggfunc='sum',\n",
    "    fill_value=0).round(2)\n",
    "fpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIRLINE  ORG_AIR\n",
       "AA       ATL         3\n",
       "         DEN         4\n",
       "         DFW        86\n",
       "         IAH         3\n",
       "         LAS         3\n",
       "                    ..\n",
       "WN       LAS         7\n",
       "         LAX        32\n",
       "         MSP         1\n",
       "         PHX         6\n",
       "         SFO        25\n",
       "Name: CANCELLED, Length: 114, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(flights\n",
    "    .groupby(['AIRLINE', 'ORG_AIR'])\n",
    "    ['CANCELLED']\n",
    "    .sum()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "fpg = (flights\n",
    "    .groupby(['AIRLINE', 'ORG_AIR'])\n",
    "    ['CANCELLED']\n",
    "    .sum()\n",
    "    .unstack('ORG_AIR', fill_value=0)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fpt.equals(fpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">sum</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">DEP_DELAY</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DIST</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>ORG_AIR</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ATL</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>...</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th>MONTH</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AA</th>\n",
       "      <th>1</th>\n",
       "      <td>-13</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1860.166667</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-39</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1337.916667</td>\n",
       "      <td>2586.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1502.758621</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1646.903226</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1436.892857</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">WN</th>\n",
       "      <th>7</th>\n",
       "      <td>2604</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>636.210526</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1718</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>644.857143</td>\n",
       "      <td>392.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1033</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>731.578947</td>\n",
       "      <td>354.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>700</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>580.875000</td>\n",
       "      <td>392.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1679</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>782.256410</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>149 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                    sum     ...         mean        \n",
       "              DEP_DELAY     ...         DIST        \n",
       "ORG_AIR             ATL     ...          SFO        \n",
       "CANCELLED             0  1  ...            0       1\n",
       "AIRLINE MONTH               ...                     \n",
       "AA      1           -13  0  ...  1860.166667     0.0\n",
       "        2           -39  0  ...  1337.916667  2586.0\n",
       "        3            -2  0  ...  1502.758621     0.0\n",
       "        4             1  0  ...  1646.903226     0.0\n",
       "        5            52  0  ...  1436.892857     0.0\n",
       "...                 ... ..  ...          ...     ...\n",
       "WN      7          2604  0  ...   636.210526     0.0\n",
       "        8          1718  0  ...   644.857143   392.0\n",
       "        9          1033  0  ...   731.578947   354.5\n",
       "        11          700  0  ...   580.875000   392.0\n",
       "        12         1679  0  ...   782.256410     0.0\n",
       "\n",
       "[149 rows x 80 columns]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights.pivot_table(index=['AIRLINE', 'MONTH'],\n",
    "    columns=['ORG_AIR', 'CANCELLED'],\n",
    "    values=['DEP_DELAY', 'DIST'],\n",
    "    aggfunc=['sum', 'mean'],\n",
    "    fill_value=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">mean</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">DEP_DELAY</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DIST</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>ORG_AIR</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ATL</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>...</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th>MONTH</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AA</th>\n",
       "      <th>1</th>\n",
       "      <td>-3.250000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>33483.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-3.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>32110.0</td>\n",
       "      <td>2586.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.166667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>43580.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.071429</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>51054.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.777778</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>40233.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">WN</th>\n",
       "      <th>7</th>\n",
       "      <td>21.700000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>24176.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>16.207547</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>18056.0</td>\n",
       "      <td>784.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>8.680672</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>27800.0</td>\n",
       "      <td>709.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5.932203</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>23235.0</td>\n",
       "      <td>784.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>15.691589</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>30508.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>149 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                    mean      ...      sum        \n",
       "               DEP_DELAY      ...     DIST        \n",
       "ORG_AIR              ATL      ...      SFO        \n",
       "CANCELLED              0   1  ...        0       1\n",
       "AIRLINE MONTH                 ...                 \n",
       "AA      1      -3.250000 NaN  ...  33483.0     NaN\n",
       "        2      -3.000000 NaN  ...  32110.0  2586.0\n",
       "        3      -0.166667 NaN  ...  43580.0     NaN\n",
       "        4       0.071429 NaN  ...  51054.0     NaN\n",
       "        5       5.777778 NaN  ...  40233.0     NaN\n",
       "...                  ...  ..  ...      ...     ...\n",
       "WN      7      21.700000 NaN  ...  24176.0     NaN\n",
       "        8      16.207547 NaN  ...  18056.0   784.0\n",
       "        9       8.680672 NaN  ...  27800.0   709.0\n",
       "        11      5.932203 NaN  ...  23235.0   784.0\n",
       "        12     15.691589 NaN  ...  30508.0     NaN\n",
       "\n",
       "[149 rows x 80 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(flights\n",
    "    .groupby(['AIRLINE', 'MONTH', 'ORG_AIR', 'CANCELLED']) \n",
    "    ['DEP_DELAY', 'DIST'] \n",
    "    .agg(['mean', 'sum']) \n",
    "    .unstack(['ORG_AIR', 'CANCELLED'], fill_value=0) \n",
    "    .swaplevel(0, 1, axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Renaming axis levels for easy reshaping"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">UGDS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>size</th>\n",
       "      <th>min</th>\n",
       "      <th>...</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>...</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>72</td>\n",
       "      <td>12.0</td>\n",
       "      <td>...</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>24</td>\n",
       "      <td>13.0</td>\n",
       "      <td>...</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WI</th>\n",
       "      <th>0</th>\n",
       "      <td>87</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>480.0</td>\n",
       "      <td>680.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>452.0</td>\n",
       "      <td>605.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WV</th>\n",
       "      <th>0</th>\n",
       "      <td>65</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>430.0</td>\n",
       "      <td>530.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>63.0</td>\n",
       "      <td>...</td>\n",
       "      <td>455.0</td>\n",
       "      <td>510.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WY</th>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "      <td>52.0</td>\n",
       "      <td>...</td>\n",
       "      <td>540.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>112 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                UGDS         ... SATMTMID       \n",
       "                size    min  ...      min    max\n",
       "STABBR RELAFFIL              ...                \n",
       "AK     0           7  109.0  ...      NaN    NaN\n",
       "       1           3   27.0  ...    503.0  503.0\n",
       "AL     0          72   12.0  ...    420.0  590.0\n",
       "       1          24   13.0  ...    400.0  560.0\n",
       "AR     0          68   18.0  ...    427.0  565.0\n",
       "...              ...    ...  ...      ...    ...\n",
       "WI     0          87   20.0  ...    480.0  680.0\n",
       "       1          25    4.0  ...    452.0  605.0\n",
       "WV     0          65   20.0  ...    430.0  530.0\n",
       "       1           8   63.0  ...    455.0  510.0\n",
       "WY     0          11   52.0  ...    540.0  540.0\n",
       "\n",
       "[112 rows x 6 columns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th colspan=\"2\" halign=\"left\">UGDS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>size</th>\n",
       "      <th>min</th>\n",
       "      <th>...</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>...</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>72</td>\n",
       "      <td>12.0</td>\n",
       "      <td>...</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>24</td>\n",
       "      <td>13.0</td>\n",
       "      <td>...</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WI</th>\n",
       "      <th>0</th>\n",
       "      <td>87</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>480.0</td>\n",
       "      <td>680.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>452.0</td>\n",
       "      <td>605.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WV</th>\n",
       "      <th>0</th>\n",
       "      <td>65</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>430.0</td>\n",
       "      <td>530.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>63.0</td>\n",
       "      <td>...</td>\n",
       "      <td>455.0</td>\n",
       "      <td>510.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WY</th>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "      <td>52.0</td>\n",
       "      <td>...</td>\n",
       "      <td>540.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>112 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS        UGDS         ... SATMTMID       \n",
       "AGG_FUNCS       size    min  ...      min    max\n",
       "STABBR RELAFFIL              ...                \n",
       "AK     0           7  109.0  ...      NaN    NaN\n",
       "       1           3   27.0  ...    503.0  503.0\n",
       "AL     0          72   12.0  ...    420.0  590.0\n",
       "       1          24   13.0  ...    400.0  560.0\n",
       "AR     0          68   18.0  ...    427.0  565.0\n",
       "...              ...    ...  ...      ...    ...\n",
       "WI     0          87   20.0  ...    480.0  680.0\n",
       "       1          25    4.0  ...    452.0  605.0\n",
       "WV     0          65   20.0  ...    430.0  530.0\n",
       "       1           8   63.0  ...    455.0  510.0\n",
       "WY     0          11   52.0  ...    540.0  540.0\n",
       "\n",
       "[112 rows x 6 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AK</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">0</th>\n",
       "      <th>size</th>\n",
       "      <td>7.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>109.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>size</th>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>27.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WV</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>min</th>\n",
       "      <td>63.0</td>\n",
       "      <td>455.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1375.0</td>\n",
       "      <td>510.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">WY</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">0</th>\n",
       "      <th>size</th>\n",
       "      <td>11.0</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>52.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>9910.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>332 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                      UGDS  SATMTMID\n",
       "STABBR RELAFFIL AGG_FUNCS                   \n",
       "AK     0        size           7.0       7.0\n",
       "                min          109.0       NaN\n",
       "                max        12865.0       NaN\n",
       "       1        size           3.0       3.0\n",
       "                min           27.0     503.0\n",
       "...                            ...       ...\n",
       "WV     1        min           63.0     455.0\n",
       "                max         1375.0     510.0\n",
       "WY     0        size          11.0      11.0\n",
       "                min           52.0     540.0\n",
       "                max         9910.0     540.0\n",
       "\n",
       "[332 rows x 2 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .stack('AGG_FUNCS')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>7.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>109.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <th>1</th>\n",
       "      <th>AK</th>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">min</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">1</th>\n",
       "      <th>AK</th>\n",
       "      <td>27.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WV</th>\n",
       "      <td>63.0</td>\n",
       "      <td>455.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <th>1</th>\n",
       "      <th>WV</th>\n",
       "      <td>1375.0</td>\n",
       "      <td>510.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <th>0</th>\n",
       "      <th>WY</th>\n",
       "      <td>11.0</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <th>0</th>\n",
       "      <th>WY</th>\n",
       "      <td>52.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <th>0</th>\n",
       "      <th>WY</th>\n",
       "      <td>9910.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>332 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                      UGDS  SATMTMID\n",
       "AGG_FUNCS RELAFFIL STABBR                   \n",
       "size      0        AK          7.0       7.0\n",
       "min       0        AK        109.0       NaN\n",
       "max       0        AK      12865.0       NaN\n",
       "size      1        AK          3.0       3.0\n",
       "min       1        AK         27.0     503.0\n",
       "...                            ...       ...\n",
       "                   WV         63.0     455.0\n",
       "max       1        WV       1375.0     510.0\n",
       "size      0        WY         11.0      11.0\n",
       "min       0        WY         52.0     540.0\n",
       "max       0        WY       9910.0     540.0\n",
       "\n",
       "[332 rows x 2 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .stack('AGG_FUNCS')\n",
    "    .swaplevel('AGG_FUNCS', 'STABBR',\n",
    "       axis='index')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>UGDS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">max</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">0</th>\n",
       "      <th>AK</th>\n",
       "      <td>NaN</td>\n",
       "      <td>12865.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AL</th>\n",
       "      <td>590.0</td>\n",
       "      <td>29851.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <td>565.0</td>\n",
       "      <td>21405.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AS</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1276.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AZ</th>\n",
       "      <td>580.0</td>\n",
       "      <td>151558.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">size</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">1</th>\n",
       "      <th>VI</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VT</th>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WA</th>\n",
       "      <td>17.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WI</th>\n",
       "      <td>25.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WV</th>\n",
       "      <td>8.0</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>332 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                   SATMTMID      UGDS\n",
       "AGG_FUNCS RELAFFIL STABBR                    \n",
       "max       0        AK           NaN   12865.0\n",
       "                   AL         590.0   29851.0\n",
       "                   AR         565.0   21405.0\n",
       "                   AS           NaN    1276.0\n",
       "                   AZ         580.0  151558.0\n",
       "...                             ...       ...\n",
       "size      1        VI           1.0       1.0\n",
       "                   VT           5.0       5.0\n",
       "                   WA          17.0      17.0\n",
       "                   WI          25.0      25.0\n",
       "                   WV           8.0       8.0\n",
       "\n",
       "[332 rows x 2 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .stack('AGG_FUNCS')\n",
    "    .swaplevel('AGG_FUNCS', 'STABBR', axis='index') \n",
    "    .sort_index(level='RELAFFIL', axis='index') \n",
    "    .sort_index(level='AGG_COLS', axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th colspan=\"2\" halign=\"left\">UGDS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>...</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>AK</th>\n",
       "      <th>AK</th>\n",
       "      <th>...</th>\n",
       "      <th>WV</th>\n",
       "      <th>WY</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>size</th>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>109.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>...</td>\n",
       "      <td>455.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>...</td>\n",
       "      <td>510.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 224 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS      UGDS         ... SATMTMID       \n",
       "RELAFFIL         0      1  ...        1      0\n",
       "STABBR          AK     AK  ...       WV     WY\n",
       "AGG_FUNCS                  ...                \n",
       "size           7.0    3.0  ...      8.0   11.0\n",
       "min          109.0   27.0  ...    455.0  540.0\n",
       "max        12865.0  275.0  ...    510.0  540.0\n",
       "\n",
       "[3 rows x 224 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .stack('AGG_FUNCS')\n",
    "    .unstack(['RELAFFIL', 'STABBR'])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "STABBR  RELAFFIL  AGG_FUNCS  AGG_COLS\n",
       "AK      0         size       UGDS            7.0\n",
       "                             SATMTMID        7.0\n",
       "                  min        UGDS          109.0\n",
       "                  max        UGDS        12865.0\n",
       "        1         size       UGDS            3.0\n",
       "                                          ...   \n",
       "WY      0         size       SATMTMID       11.0\n",
       "                  min        UGDS           52.0\n",
       "                             SATMTMID      540.0\n",
       "                  max        UGDS         9910.0\n",
       "                             SATMTMID      540.0\n",
       "Length: 640, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .stack(['AGG_FUNCS', 'AGG_COLS'])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AGG_COLS  AGG_FUNCS  STABBR  RELAFFIL\n",
       "UGDS      size       AK      0             7.0\n",
       "                             1             3.0\n",
       "                     AL      0            72.0\n",
       "                             1            24.0\n",
       "                     AR      0            68.0\n",
       "                                         ...  \n",
       "SATMTMID  max        WI      1           605.0\n",
       "                     WV      0           530.0\n",
       "                             1           510.0\n",
       "                     WY      0           540.0\n",
       "                             1             NaN\n",
       "Length: 708, dtype: float64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "    .unstack(['STABBR', 'RELAFFIL']) \n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">UGDS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>size</th>\n",
       "      <th>min</th>\n",
       "      <th>...</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>...</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>72</td>\n",
       "      <td>12.0</td>\n",
       "      <td>...</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>24</td>\n",
       "      <td>13.0</td>\n",
       "      <td>...</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WI</th>\n",
       "      <th>0</th>\n",
       "      <td>87</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>480.0</td>\n",
       "      <td>680.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>452.0</td>\n",
       "      <td>605.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">WV</th>\n",
       "      <th>0</th>\n",
       "      <td>65</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>430.0</td>\n",
       "      <td>530.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>63.0</td>\n",
       "      <td>...</td>\n",
       "      <td>455.0</td>\n",
       "      <td>510.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WY</th>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "      <td>52.0</td>\n",
       "      <td>...</td>\n",
       "      <td>540.0</td>\n",
       "      <td>540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>112 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     UGDS         ... SATMTMID       \n",
       "     size    min  ...      min    max\n",
       "AK 0    7  109.0  ...      NaN    NaN\n",
       "   1    3   27.0  ...    503.0  503.0\n",
       "AL 0   72   12.0  ...    420.0  590.0\n",
       "   1   24   13.0  ...    400.0  560.0\n",
       "AR 0   68   18.0  ...    427.0  565.0\n",
       "...   ...    ...  ...      ...    ...\n",
       "WI 0   87   20.0  ...    480.0  680.0\n",
       "   1   25    4.0  ...    452.0  605.0\n",
       "WV 0   65   20.0  ...    430.0  530.0\n",
       "   1    8   63.0  ...    455.0  510.0\n",
       "WY 0   11   52.0  ...    540.0  540.0\n",
       "\n",
       "[112 rows x 6 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(college\n",
    "    .groupby(['STABBR', 'RELAFFIL']) \n",
    "    ['UGDS', 'SATMTMID'] \n",
    "    .agg(['size', 'min', 'max'])\n",
    "    .rename_axis([None, None], axis='index') \n",
    "    .rename_axis([None, None], axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying when multiple variables are stored as column names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>M35 35-39</th>\n",
       "      <th>...</th>\n",
       "      <th>M75 75-79</th>\n",
       "      <th>M80 80+</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "      <td>...</td>\n",
       "      <td>62</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "      <td>...</td>\n",
       "      <td>67</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "      <td>...</td>\n",
       "      <td>75</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "      <td>...</td>\n",
       "      <td>82</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "      <td>...</td>\n",
       "      <td>87</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>94</td>\n",
       "      <td>202</td>\n",
       "      <td>...</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>105</td>\n",
       "      <td>210</td>\n",
       "      <td>...</td>\n",
       "      <td>95</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>105+</td>\n",
       "      <td>217</td>\n",
       "      <td>...</td>\n",
       "      <td>100</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  Weight Category  M35 35-39  ...  M75 75-79  M80 80+\n",
       "0           56           137  ...         62       55\n",
       "1           62           152  ...         67       57\n",
       "2           69           167  ...         75       60\n",
       "3           77           182  ...         82       65\n",
       "4           85           192  ...         87       70\n",
       "5           94           202  ...         90       75\n",
       "6          105           210  ...         95       80\n",
       "7         105+           217  ...        100       85\n",
       "\n",
       "[8 rows x 11 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weightlifting = pd.read_csv('data/weightlifting_men.csv')\n",
    "weightlifting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>sex_age</th>\n",
       "      <th>Qual Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>77</td>\n",
       "      <td>M80 80+</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>85</td>\n",
       "      <td>M80 80+</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>94</td>\n",
       "      <td>M80 80+</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>105</td>\n",
       "      <td>M80 80+</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>105+</td>\n",
       "      <td>M80 80+</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Weight Category    sex_age  Qual Total\n",
       "0            56     M35 35-39         137\n",
       "1            62     M35 35-39         152\n",
       "2            69     M35 35-39         167\n",
       "3            77     M35 35-39         182\n",
       "4            85     M35 35-39         192\n",
       "..          ...           ...         ...\n",
       "75           77       M80 80+          65\n",
       "76           85       M80 80+          70\n",
       "77           94       M80 80+          75\n",
       "78          105       M80 80+          80\n",
       "79         105+       M80 80+          85\n",
       "\n",
       "[80 rows x 3 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      0      1\n",
       "0   M35  35-39\n",
       "1   M35  35-39\n",
       "2   M35  35-39\n",
       "3   M35  35-39\n",
       "4   M35  35-39\n",
       "..  ...    ...\n",
       "75  M80    80+\n",
       "76  M80    80+\n",
       "77  M80    80+\n",
       "78  M80    80+\n",
       "79  M80    80+\n",
       "\n",
       "[80 rows x 2 columns]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    "    ['sex_age']\n",
    "    .str.split(expand=True)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>M80</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Sex Age Group\n",
       "0   M35     35-39\n",
       "1   M35     35-39\n",
       "2   M35     35-39\n",
       "3   M35     35-39\n",
       "4   M35     35-39\n",
       "..  ...       ...\n",
       "75  M80       80+\n",
       "76  M80       80+\n",
       "77  M80       80+\n",
       "78  M80       80+\n",
       "79  M80       80+\n",
       "\n",
       "[80 rows x 2 columns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    "    ['sex_age']\n",
    "    .str.split(expand=True)\n",
    "    .rename(columns={0:'Sex', 1:'Age Group'})\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Sex Age Group\n",
       "0    M     35-39\n",
       "1    M     35-39\n",
       "2    M     35-39\n",
       "3    M     35-39\n",
       "4    M     35-39\n",
       "..  ..       ...\n",
       "75   M       80+\n",
       "76   M       80+\n",
       "77   M       80+\n",
       "78   M       80+\n",
       "79   M       80+\n",
       "\n",
       "[80 rows x 2 columns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    "    ['sex_age']\n",
    "    .str.split(expand=True)\n",
    "    .rename(columns={0:'Sex', 1:'Age Group'})\n",
    "    .assign(Sex=lambda df_: df_.Sex.str[0])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>Qual Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>77</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>85</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>94</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>105</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>105+</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Sex Age Group Weight Category  Qual Total\n",
       "0    M     35-39           56            137\n",
       "1    M     35-39           62            152\n",
       "2    M     35-39           69            167\n",
       "3    M     35-39           77            182\n",
       "4    M     35-39           85            192\n",
       "..  ..       ...          ...            ...\n",
       "75   M       80+           77             65\n",
       "76   M       80+           85             70\n",
       "77   M       80+           94             75\n",
       "78   M       80+          105             80\n",
       "79   M       80+         105+             85\n",
       "\n",
       "[80 rows x 4 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted = (weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    ")\n",
    "tidy = pd.concat([melted\n",
    "           ['sex_age']\n",
    "           .str.split(expand=True)\n",
    "           .rename(columns={0:'Sex', 1:'Age Group'})\n",
    "           .assign(Sex=lambda df_: df_.Sex.str[0]),\n",
    "          melted[['Weight Category', 'Qual Total']]],\n",
    "          axis='columns'\n",
    ")\n",
    "tidy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "      <th>Category</th>\n",
       "      <th>Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>77</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>85</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>94</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>105</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "      <td>105+</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Sex Age Group Category  Total\n",
       "0    M     35-39       56    137\n",
       "1    M     35-39       62    152\n",
       "2    M     35-39       69    167\n",
       "3    M     35-39       77    182\n",
       "4    M     35-39       85    192\n",
       "..  ..       ...      ...    ...\n",
       "75   M       80+       77     65\n",
       "76   M       80+       85     70\n",
       "77   M       80+       94     75\n",
       "78   M       80+      105     80\n",
       "79   M       80+     105+     85\n",
       "\n",
       "[80 rows x 4 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted = (weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    ")\n",
    "(melted\n",
    "    ['sex_age']\n",
    "    .str.split(expand=True)\n",
    "    .rename(columns={0:'Sex', 1:'Age Group'})\n",
    "    .assign(Sex=lambda df_: df_.Sex.str[0],\n",
    "            Category=melted['Weight Category'],\n",
    "            Total=melted['Qual Total'])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "tidy2 = (weightlifting\n",
    "    .melt(id_vars='Weight Category',\n",
    "          var_name='sex_age',\n",
    "          value_name='Qual Total')\n",
    "    .assign(Sex=lambda df_:df_.sex_age.str[0],\n",
    "            **{'Age Group':(lambda df_: (df_\n",
    "                .sex_age\n",
    "                .str.extract(r'(\\d{2}[-+](?:\\d{2})?)',\n",
    "                             expand=False)))})\n",
    "    .drop(columns='sex_age')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>Qual Total</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>77</td>\n",
       "      <td>65</td>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>85</td>\n",
       "      <td>70</td>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>94</td>\n",
       "      <td>75</td>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>105</td>\n",
       "      <td>80</td>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>105+</td>\n",
       "      <td>85</td>\n",
       "      <td>M</td>\n",
       "      <td>80+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>80 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Weight Category  Qual Total Sex Age Group\n",
       "0            56            137   M     35-39\n",
       "1            62            152   M     35-39\n",
       "2            69            167   M     35-39\n",
       "3            77            182   M     35-39\n",
       "4            85            192   M     35-39\n",
       "..          ...            ...  ..       ...\n",
       "75           77             65   M       80+\n",
       "76           85             70   M       80+\n",
       "77           94             75   M       80+\n",
       "78          105             80   M       80+\n",
       "79         105+             85   M       80+\n",
       "\n",
       "[80 rows x 4 columns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tidy2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tidy.sort_index(axis=1).equals(tidy2.sort_index(axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying when multiple variables are stored is a single column"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Info</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>E &amp; E Gr...</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Borough</td>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>E &amp; E Gr...</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Cuisine</td>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>E &amp; E Gr...</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Description</td>\n",
       "      <td>Non-food...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>E &amp; E Gr...</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Grade</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E &amp; E Gr...</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Score</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>495</th>\n",
       "      <td>PIER SIX...</td>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>Borough</td>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496</th>\n",
       "      <td>PIER SIX...</td>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>Cuisine</td>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>497</th>\n",
       "      <td>PIER SIX...</td>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>Description</td>\n",
       "      <td>Filth fl...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>498</th>\n",
       "      <td>PIER SIX...</td>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>Grade</td>\n",
       "      <td>Z</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>PIER SIX...</td>\n",
       "      <td>2017-09-01</td>\n",
       "      <td>Score</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Name       Date         Info        Value\n",
       "0    E & E Gr... 2017-08-08      Borough    MANHATTAN\n",
       "1    E & E Gr... 2017-08-08      Cuisine     American\n",
       "2    E & E Gr... 2017-08-08  Description  Non-food...\n",
       "3    E & E Gr... 2017-08-08        Grade            A\n",
       "4    E & E Gr... 2017-08-08        Score          9.0\n",
       "..           ...        ...          ...          ...\n",
       "495  PIER SIX... 2017-09-01      Borough    MANHATTAN\n",
       "496  PIER SIX... 2017-09-01      Cuisine     American\n",
       "497  PIER SIX... 2017-09-01  Description  Filth fl...\n",
       "498  PIER SIX... 2017-09-01        Grade            Z\n",
       "499  PIER SIX... 2017-09-01        Score         33.0\n",
       "\n",
       "[500 rows x 4 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections = pd.read_csv('data/restaurant_inspections.csv',\n",
    "    parse_dates=['Date'])\n",
    "inspections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "ename": "NotImplementedError",
     "evalue": "> 1 ndim Categorical are not supported at this time",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/arrays/categorical.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, values, categories, ordered, dtype, fastpath)\u001b[0m\n\u001b[1;32m    383\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 384\u001b[0;31m                 \u001b[0mcodes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcategories\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfactorize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    385\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/util/_decorators.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    207\u001b[0m                     \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnew_arg_name\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnew_arg_value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 208\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    209\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/algorithms.py\u001b[0m in \u001b[0;36mfactorize\u001b[0;34m(values, sort, order, na_sentinel, size_hint)\u001b[0m\n\u001b[1;32m    671\u001b[0m         labels, uniques = _factorize_array(\n\u001b[0;32m--> 672\u001b[0;31m             \u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mna_sentinel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mna_sentinel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize_hint\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msize_hint\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mna_value\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mna_value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    673\u001b[0m         )\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/algorithms.py\u001b[0m in \u001b[0;36m_factorize_array\u001b[0;34m(values, na_sentinel, size_hint, na_value)\u001b[0m\n\u001b[1;32m    507\u001b[0m     uniques, labels = table.factorize(\n\u001b[0;32m--> 508\u001b[0;31m         \u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mna_sentinel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mna_sentinel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mna_value\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mna_value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    509\u001b[0m     )\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.factorize\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Buffer has wrong number of dimensions (expected 1, got 2)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mNotImplementedError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-64-3cf88d419a7c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m inspections.pivot(index=['Name', 'Date'],\n\u001b[0;32m----> 2\u001b[0;31m     columns='Info', values='Value')\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(self, index, columns, values)\u001b[0m\n\u001b[1;32m   5932\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5933\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5934\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   5935\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5936\u001b[0m     _shared_docs[\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/reshape/pivot.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(data, index, columns, values)\u001b[0m\n\u001b[1;32m    419\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    420\u001b[0m             \u001b[0mindex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 421\u001b[0;31m         \u001b[0mindex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMultiIndex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_arrays\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    422\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    423\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mis_list_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexes/multi.py\u001b[0m in \u001b[0;36mfrom_arrays\u001b[0;34m(cls, arrays, sortorder, names)\u001b[0m\n\u001b[1;32m    418\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marrays\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcategorical\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0m_factorize_from_iterables\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    419\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 420\u001b[0;31m         \u001b[0mcodes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_factorize_from_iterables\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    421\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mnames\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    422\u001b[0m             \u001b[0mnames\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"name\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0marr\u001b[0m \u001b[0;32min\u001b[0m \u001b[0marrays\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/arrays/categorical.py\u001b[0m in \u001b[0;36m_factorize_from_iterables\u001b[0;34m(iterables)\u001b[0m\n\u001b[1;32m   2814\u001b[0m         \u001b[0;31m# For consistency, it should return a list of 2 lists.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2815\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2816\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_factorize_from_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0miterables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/arrays/categorical.py\u001b[0m in \u001b[0;36m<genexpr>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m   2814\u001b[0m         \u001b[0;31m# For consistency, it should return a list of 2 lists.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2815\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2816\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_factorize_from_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0miterables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/arrays/categorical.py\u001b[0m in \u001b[0;36m_factorize_from_iterable\u001b[0;34m(values)\u001b[0m\n\u001b[1;32m   2786\u001b[0m         \u001b[0;31m# but only the resulting categories, the order of which is independent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2787\u001b[0m         \u001b[0;31m# from ordered. Set ordered to False as default. See GH #15457\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2788\u001b[0;31m         \u001b[0mcat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCategorical\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mordered\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2789\u001b[0m         \u001b[0mcategories\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcategories\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2790\u001b[0m         \u001b[0mcodes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcodes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/arrays/categorical.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, values, categories, ordered, dtype, fastpath)\u001b[0m\n\u001b[1;32m    397\u001b[0m                 \u001b[0;31m# FIXME\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    398\u001b[0m                 raise NotImplementedError(\n\u001b[0;32m--> 399\u001b[0;31m                     \u001b[0;34m\"> 1 ndim Categorical are not \"\u001b[0m \u001b[0;34m\"supported at this time\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    400\u001b[0m                 )\n\u001b[1;32m    401\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNotImplementedError\u001b[0m: > 1 ndim Categorical are not supported at this time"
     ]
    }
   ],
   "source": [
    "inspections.pivot(index=['Name', 'Date'],\n",
    "    columns='Info', values='Value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Info</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">E &amp; E Grill House</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-08-08</th>\n",
       "      <th>Borough</th>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cuisine</th>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Description</th>\n",
       "      <td>Non-food...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Grade</th>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Score</th>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">PIER SIXTY ONE-THE LIGHTHOUSE</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-09-01</th>\n",
       "      <th>Borough</th>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cuisine</th>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Description</th>\n",
       "      <td>Filth fl...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Grade</th>\n",
       "      <td>Z</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Score</th>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           Value\n",
       "Name         Date       Info                    \n",
       "E & E Gri... 2017-08-08 Borough        MANHATTAN\n",
       "                        Cuisine         American\n",
       "                        Description  Non-food...\n",
       "                        Grade                  A\n",
       "                        Score                9.0\n",
       "...                                          ...\n",
       "PIER SIXT... 2017-09-01 Borough        MANHATTAN\n",
       "                        Cuisine         American\n",
       "                        Description  Filth fl...\n",
       "                        Grade                  Z\n",
       "                        Score               33.0\n",
       "\n",
       "[500 rows x 1 columns]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections.set_index(['Name','Date', 'Info'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Info</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>...</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3 STAR JUICE CENTER</th>\n",
       "      <th>2017-05-10</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, S...</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A &amp; L PIZZA RESTAURANT</th>\n",
       "      <th>2017-08-22</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AKSARAY TURKISH CAFE AND RESTAURANT</th>\n",
       "      <th>2017-07-25</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ANTOJITOS DELI FOOD</th>\n",
       "      <th>2017-06-01</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (C...</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BANGIA</th>\n",
       "      <th>2017-06-16</th>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VALL'S PIZZERIA</th>\n",
       "      <th>2017-03-15</th>\n",
       "      <td>STATEN I...</td>\n",
       "      <td>Pizza/It...</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VIP GRILL</th>\n",
       "      <th>2017-06-12</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/K...</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WAHIZZA</th>\n",
       "      <th>2017-04-13</th>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WANG MANDOO HOUSE</th>\n",
       "      <th>2017-08-29</th>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>XIAOYAN YABO INC</th>\n",
       "      <th>2017-08-29</th>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                               Value               ...            \n",
       "Info                         Borough      Cuisine  ... Grade Score\n",
       "Name         Date                                  ...            \n",
       "3 STAR JU... 2017-05-10     BROOKLYN  Juice, S...  ...     A  12.0\n",
       "A & L PIZ... 2017-08-22     BROOKLYN        Pizza  ...     A   9.0\n",
       "AKSARAY T... 2017-07-25     BROOKLYN      Turkish  ...     A  13.0\n",
       "ANTOJITOS... 2017-06-01     BROOKLYN  Latin (C...  ...     A  10.0\n",
       "BANGIA       2017-06-16    MANHATTAN       Korean  ...     A   9.0\n",
       "...                              ...          ...  ...   ...   ...\n",
       "VALL'S PI... 2017-03-15  STATEN I...  Pizza/It...  ...     A   9.0\n",
       "VIP GRILL    2017-06-12     BROOKLYN  Jewish/K...  ...     A  10.0\n",
       "WAHIZZA      2017-04-13    MANHATTAN        Pizza  ...     A  10.0\n",
       "WANG MAND... 2017-08-29       QUEENS       Korean  ...     A  12.0\n",
       "XIAOYAN Y... 2017-08-29       QUEENS       Korean  ...     Z  49.0\n",
       "\n",
       "[100 rows x 5 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(inspections\n",
    "    .set_index(['Name','Date', 'Info']) \n",
    "    .unstack('Info')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\"></th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Info</th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>...</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR J...</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PI...</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY ...</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITO...</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S P...</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MAN...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN ...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                              ... Value      \n",
       "Info         Name       Date  ... Grade Score\n",
       "0     3 STAR J... 2017-05-10  ...     A  12.0\n",
       "1     A & L PI... 2017-08-22  ...     A   9.0\n",
       "2     AKSARAY ... 2017-07-25  ...     A  13.0\n",
       "3     ANTOJITO... 2017-06-01  ...     A  10.0\n",
       "4          BANGIA 2017-06-16  ...     A   9.0\n",
       "..            ...        ...  ...   ...   ...\n",
       "95    VALL'S P... 2017-03-15  ...     A   9.0\n",
       "96      VIP GRILL 2017-06-12  ...     A  10.0\n",
       "97        WAHIZZA 2017-04-13  ...     A  10.0\n",
       "98    WANG MAN... 2017-08-29  ...     A  12.0\n",
       "99    XIAOYAN ... 2017-08-29  ...     Z  49.0\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(inspections\n",
    "    .set_index(['Name','Date', 'Info']) \n",
    "    .unstack('Info')\n",
    "    .reset_index(col_level=-1)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "def flatten0(df_):\n",
    "    df_.columns = df_.columns.droplevel(0).rename(None)\n",
    "    return df_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>...</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR J...</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PI...</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY ...</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITO...</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S P...</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MAN...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN ...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Name       Date  ... Grade Score\n",
       "0   3 STAR J... 2017-05-10  ...     A  12.0\n",
       "1   A & L PI... 2017-08-22  ...     A   9.0\n",
       "2   AKSARAY ... 2017-07-25  ...     A  13.0\n",
       "3   ANTOJITO... 2017-06-01  ...     A  10.0\n",
       "4        BANGIA 2017-06-16  ...     A   9.0\n",
       "..          ...        ...  ...   ...   ...\n",
       "95  VALL'S P... 2017-03-15  ...     A   9.0\n",
       "96    VIP GRILL 2017-06-12  ...     A  10.0\n",
       "97      WAHIZZA 2017-04-13  ...     A  10.0\n",
       "98  WANG MAN... 2017-08-29  ...     A  12.0\n",
       "99  XIAOYAN ... 2017-08-29  ...     Z  49.0\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(inspections\n",
    "    .set_index(['Name','Date', 'Info']) \n",
    "    .unstack('Info')\n",
    "    .reset_index(col_level=-1)\n",
    "    .pipe(flatten0)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>...</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR J...</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PI...</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY ...</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITO...</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S P...</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MAN...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN ...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Name       Date  ... Grade Score\n",
       "0   3 STAR J... 2017-05-10  ...     A  12.0\n",
       "1   A & L PI... 2017-08-22  ...     A   9.0\n",
       "2   AKSARAY ... 2017-07-25  ...     A  13.0\n",
       "3   ANTOJITO... 2017-06-01  ...     A  10.0\n",
       "4        BANGIA 2017-06-16  ...     A   9.0\n",
       "..          ...        ...  ...   ...   ...\n",
       "95  VALL'S P... 2017-03-15  ...     A   9.0\n",
       "96    VIP GRILL 2017-06-12  ...     A  10.0\n",
       "97      WAHIZZA 2017-04-13  ...     A  10.0\n",
       "98  WANG MAN... 2017-08-29  ...     A  12.0\n",
       "99  XIAOYAN ... 2017-08-29  ...     Z  49.0\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(inspections\n",
    "    .set_index(['Name','Date', 'Info']) \n",
    "    .squeeze() \n",
    "    .unstack('Info') \n",
    "    .reset_index() \n",
    "    .rename_axis(None, axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>...</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR J...</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PI...</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY ...</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITO...</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S P...</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MAN...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN ...</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           Name       Date  ... Grade Score\n",
       "0   3 STAR J... 2017-05-10  ...     A  12.0\n",
       "1   A & L PI... 2017-08-22  ...     A   9.0\n",
       "2   AKSARAY ... 2017-07-25  ...     A  13.0\n",
       "3   ANTOJITO... 2017-06-01  ...     A  10.0\n",
       "4        BANGIA 2017-06-16  ...     A   9.0\n",
       "..          ...        ...  ...   ...   ...\n",
       "95  VALL'S P... 2017-03-15  ...     A   9.0\n",
       "96    VIP GRILL 2017-06-12  ...     A  10.0\n",
       "97      WAHIZZA 2017-04-13  ...     A  10.0\n",
       "98  WANG MAN... 2017-08-29  ...     A  12.0\n",
       "99  XIAOYAN ... 2017-08-29  ...     Z  49.0\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(inspections\n",
    "    .pivot_table(index=['Name', 'Date'],\n",
    "                 columns='Info',\n",
    "                 values='Value',\n",
    "                 aggfunc='first') \n",
    "    .reset_index() \n",
    "    .rename_axis(None, axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying when two or more values are stored in the same cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it.."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>City</th>\n",
       "      <th>Geolocation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Houston</td>\n",
       "      <td>29.7604°...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Dallas</td>\n",
       "      <td>32.7767°...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Austin</td>\n",
       "      <td>30.2672°...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      City  Geolocation\n",
       "0  Houston  29.7604°...\n",
       "1   Dallas  32.7767°...\n",
       "2   Austin  30.2672°..."
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities = pd.read_csv('data/texas_cities.csv')\n",
    "cities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "geolocations = cities.Geolocation.str.split(pat='. ',\n",
    "    expand=True)\n",
    "geolocations.columns = ['latitude', 'latitude direction',\n",
    "    'longitude', 'longitude direction']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "latitude               float64\n",
       "latitude direction      object\n",
       "longitude              float64\n",
       "longitude direction     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolocations = geolocations.astype({'latitude':'float',\n",
    "   'longitude':'float'})\n",
    "geolocations.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>...</th>\n",
       "      <th>longitude direction</th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>...</td>\n",
       "      <td>W</td>\n",
       "      <td>Houston</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>...</td>\n",
       "      <td>W</td>\n",
       "      <td>Dallas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>...</td>\n",
       "      <td>W</td>\n",
       "      <td>Austin</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   latitude latitude direction  ...  longitude direction     city\n",
       "0   29.7604            N        ...            W          Houston\n",
       "1   32.7767            N        ...            W           Dallas\n",
       "2   30.2672            N        ...            W           Austin\n",
       "\n",
       "[3 rows x 5 columns]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(geolocations\n",
    "    .assign(city=cities['City'])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>longitude</th>\n",
       "      <th>longitude direction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   latitude latitude direction  longitude longitude direction\n",
       "0   29.7604            N          95.3698            W       \n",
       "1   32.7767            N          96.7970            W       \n",
       "2   30.2672            N          97.7431            W       "
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolocations.apply(pd.to_numeric, errors='ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0  1        2  3\n",
       "0  29.7604  N  95.3698  W\n",
       "1  32.7767  N  96.7970  W\n",
       "2  30.2672  N  97.7431  W"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities.Geolocation.str.split(pat=r'° |, ', expand=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0  1        2  3\n",
       "0  29.7604  N  95.3698  W\n",
       "1  32.7767  N  96.7970  W\n",
       "2  30.2672  N  97.7431  W"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities.Geolocation.str.extract(r'([0-9.]+). (N|S), ([0-9.]+). (E|W)',\n",
    "   expand=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tidying when variables are stored in column names and values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Getting ready"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Property</th>\n",
       "      <th>...</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>...</td>\n",
       "      <td>973</td>\n",
       "      <td>870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>...</td>\n",
       "      <td>1036</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Flow</td>\n",
       "      <td>...</td>\n",
       "      <td>882</td>\n",
       "      <td>856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>...</td>\n",
       "      <td>806</td>\n",
       "      <td>942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>...</td>\n",
       "      <td>1002</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>Flow</td>\n",
       "      <td>...</td>\n",
       "      <td>824</td>\n",
       "      <td>873</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group     Property  ...  2015  2016\n",
       "0     A     Pressure  ...   973   870\n",
       "1     A  Temperature  ...  1036  1042\n",
       "2     A         Flow  ...   882   856\n",
       "3     B     Pressure  ...   806   942\n",
       "4     B  Temperature  ...  1002  1013\n",
       "5     B         Flow  ...   824   873\n",
       "\n",
       "[6 rows x 7 columns]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors = pd.read_csv('data/sensors.csv')\n",
    "sensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Property</th>\n",
       "      <th>Year</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>2012</td>\n",
       "      <td>928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2012</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Flow</td>\n",
       "      <td>2012</td>\n",
       "      <td>819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>2012</td>\n",
       "      <td>817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2012</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2016</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>A</td>\n",
       "      <td>Flow</td>\n",
       "      <td>2016</td>\n",
       "      <td>856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>B</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>2016</td>\n",
       "      <td>942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>B</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2016</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>B</td>\n",
       "      <td>Flow</td>\n",
       "      <td>2016</td>\n",
       "      <td>873</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Group     Property  Year  value\n",
       "0      A     Pressure  2012    928\n",
       "1      A  Temperature  2012   1026\n",
       "2      A         Flow  2012    819\n",
       "3      B     Pressure  2012    817\n",
       "4      B  Temperature  2012   1008\n",
       "..   ...          ...   ...    ...\n",
       "25     A  Temperature  2016   1042\n",
       "26     A         Flow  2016    856\n",
       "27     B     Pressure  2016    942\n",
       "28     B  Temperature  2016   1013\n",
       "29     B         Flow  2016    873\n",
       "\n",
       "[30 rows x 4 columns]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors.melt(id_vars=['Group', 'Property'], var_name='Year')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Year</th>\n",
       "      <th>...</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Temperature</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>2012</td>\n",
       "      <td>...</td>\n",
       "      <td>928</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>2013</td>\n",
       "      <td>...</td>\n",
       "      <td>873</td>\n",
       "      <td>1038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>2014</td>\n",
       "      <td>...</td>\n",
       "      <td>814</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>973</td>\n",
       "      <td>1036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>2016</td>\n",
       "      <td>...</td>\n",
       "      <td>870</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>2012</td>\n",
       "      <td>...</td>\n",
       "      <td>817</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>B</td>\n",
       "      <td>2013</td>\n",
       "      <td>...</td>\n",
       "      <td>877</td>\n",
       "      <td>1041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>2014</td>\n",
       "      <td>...</td>\n",
       "      <td>914</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>806</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>2016</td>\n",
       "      <td>...</td>\n",
       "      <td>942</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group  Year  ...  Pressure  Temperature\n",
       "0     A  2012  ...       928         1026\n",
       "1     A  2013  ...       873         1038\n",
       "2     A  2014  ...       814         1009\n",
       "3     A  2015  ...       973         1036\n",
       "4     A  2016  ...       870         1042\n",
       "5     B  2012  ...       817         1008\n",
       "6     B  2013  ...       877         1041\n",
       "7     B  2014  ...       914         1009\n",
       "8     B  2015  ...       806         1002\n",
       "9     B  2016  ...       942         1013\n",
       "\n",
       "[10 rows x 5 columns]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sensors\n",
    "    .melt(id_vars=['Group', 'Property'], var_name='Year') \n",
    "    .pivot_table(index=['Group', 'Year'],\n",
    "                 columns='Property', values='value') \n",
    "    .reset_index() \n",
    "    .rename_axis(None, axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Year</th>\n",
       "      <th>...</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Temperature</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>2012</td>\n",
       "      <td>...</td>\n",
       "      <td>928</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>2013</td>\n",
       "      <td>...</td>\n",
       "      <td>873</td>\n",
       "      <td>1038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>2014</td>\n",
       "      <td>...</td>\n",
       "      <td>814</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>973</td>\n",
       "      <td>1036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>2016</td>\n",
       "      <td>...</td>\n",
       "      <td>870</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>2012</td>\n",
       "      <td>...</td>\n",
       "      <td>817</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>B</td>\n",
       "      <td>2013</td>\n",
       "      <td>...</td>\n",
       "      <td>877</td>\n",
       "      <td>1041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>2014</td>\n",
       "      <td>...</td>\n",
       "      <td>914</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>806</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>2016</td>\n",
       "      <td>...</td>\n",
       "      <td>942</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group  Year  ...  Pressure  Temperature\n",
       "0     A  2012  ...       928         1026\n",
       "1     A  2013  ...       873         1038\n",
       "2     A  2014  ...       814         1009\n",
       "3     A  2015  ...       973         1036\n",
       "4     A  2016  ...       870         1042\n",
       "5     B  2012  ...       817         1008\n",
       "6     B  2013  ...       877         1041\n",
       "7     B  2014  ...       914         1009\n",
       "8     B  2015  ...       806         1002\n",
       "9     B  2016  ...       942         1013\n",
       "\n",
       "[10 rows x 5 columns]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sensors\n",
    "    .set_index(['Group', 'Property']) \n",
    "    .stack() \n",
    "    .unstack('Property') \n",
    "    .rename_axis(['Group', 'Year'], axis='index') \n",
    "    .rename_axis(None, axis='columns') \n",
    "    .reset_index()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
